package com.hn.gege.sys.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hn.gege.sys.modules.sys.mapper.MenuMapper;
import com.hn.gege.sys.modules.sys.dto.MenuTreeDTO;
import com.hn.gege.sys.modules.sys.entity.Menu;
import com.hn.gege.sys.modules.sys.service.IMenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author zxy
 * @since 2020-08-07
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Override
    public Menu getByPk(String id) {
        LambdaQueryWrapper<Menu> lambda = new QueryWrapper<Menu>().lambda()
                .eq(Menu::getId, id);
        return baseMapper.selectOne(lambda);
    }

    @Override
    public boolean updateByPk(Menu menu) {
        LambdaQueryWrapper<Menu> lambda = new QueryWrapper<Menu>().lambda()
                .eq(Menu::getId, menu.getId());
        return super.update(menu, lambda);
    }

    @Override
    public boolean removeByPk(String id) {
        LambdaQueryWrapper<Menu> lambda = new QueryWrapper<Menu>().lambda()
                .eq(Menu::getId, id);
        return super.remove(lambda);
    }

    @Override
    public List<MenuTreeDTO> tree(String category) {
        List<MenuTreeDTO> menuTreeDTOS = baseMapper.selectTreeByParentId(category);
        return treeAll(menuTreeDTOS);
    }

    private List<MenuTreeDTO> treeAll(List<MenuTreeDTO> list){
        if (list == null || list.size()==0)
            return null;

        list.forEach(item -> {
            List<MenuTreeDTO> menuTreeDTOS = baseMapper.selectTreeByParentId(item.getId());
            item.setChildren(treeAll(menuTreeDTOS));
        });
        return list;
    }


}
